package com.hk.webapp.controller.admin;

import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import com.hk.webapp.bean.CareerCity;
import com.hk.webapp.bean.CareerInfo;
import com.hk.webapp.bean.CareerJob;
import com.hk.webapp.bean.CareerType;
import com.hk.webapp.bo.CareerInfoBo;
import com.hk.webapp.service.*;
import com.hk.webapp.vo.EditCareerVo;
import com.hk.webapp.vo.IdsVo;
import com.hk.webapp.vo.PageVo;
import com.hk.webapp.vo.ResultVo;
import com.hk.webapp.service.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@RestController
public class CareerController extends AdminBaseController {
    @Resource
    private CareerService careerService;
    @Resource
    private CareerInfoService careerInfoService;
    @Resource
    private CareerCityService careerCityService;
    @Resource
    private CareerJobService careerJobService;
    @Resource
    private CareerTypeService careerTypeService;

    @PostMapping("/Career/GetPageList")
    public ResultVo GetPageList(@RequestBody PageVo vo){
        PageInfo<CareerInfoBo> plist= careerInfoService.GetCareerList(vo.getPageIndex(),vo.getPageSize(),0,0,0,0);
        return ResultVo.createBySuccessData(plist.getList(), plist.getTotal());
    }

    @PostMapping("/Career/GetCareerEnum")
    public ResultVo GetCareerEnum(){
        List<CareerCity> career_city_list = careerCityService.GetEntityList();
        List<CareerJob> career_job_list = careerJobService.GetEntityList();
        List<CareerType> career_type_list = careerTypeService.GetEntityList();

        HashMap<String,Object> map= Maps.newHashMap();
        map.put("cityList",career_city_list.parallelStream().map(c->{
            HashMap<String,Object> _map=Maps.newHashMap();
            _map.put("id",c.getId());
            _map.put("name",c.getName());
            return _map;
        }).collect(Collectors.toList()));
        map.put("jobList",career_job_list.parallelStream().map(c->{
            HashMap<String,Object> _map=Maps.newHashMap();
            _map.put("id",c.getId());
            _map.put("name",c.getName());
            return _map;
        }).collect(Collectors.toList()));
        map.put("typeList",career_type_list.parallelStream().map(c->{
            HashMap<String,Object> _map=Maps.newHashMap();
            _map.put("id",c.getId());
            _map.put("name",c.getName());
            return _map;
        }).collect(Collectors.toList()));

        return ResultVo.createBySuccessData(map);
    }

    @PostMapping("/Career/AddCareer")
    public ResultVo AddCareer(@RequestBody EditCareerVo vo){
        CareerInfo careerInfo=new CareerInfo();
        careerInfo.setCityId(vo.getCity_id());
        careerInfo.setCompany(vo.getCompany());
        careerInfo.setDutiesContent(vo.getDuties_content());
        careerInfo.setQualifiedContent(vo.getQualified_content());
        careerInfo.setJobId(vo.getJob_id());
        careerInfo.setStatus(1);
        careerInfo.setTitle(vo.getTitle());
        careerInfo.setTypeId(vo.getType_id());
        careerInfo.setIsDeleted(0);
        careerInfoService.insert(careerInfo);
        if (careerInfo.getId()<=0){
            return ResultVo.createByError("添加失败");
        }
        return ResultVo.createBySuccessData("succeed");
    }

    @PostMapping("/Career/Detail")
    public ResultVo Detail(Integer id){
        if (id<=0){
            return ResultVo.createByError("数据不存在或已删除");
        }
        CareerInfo info = careerInfoService.GetEntityById(id);
        if (info == null)
        {
            return ResultVo.createByError("数据不存在或已删除");
        }
        return ResultVo.createBySuccessData(info);
    }
    @PostMapping("/Career/EditCareer")
    public ResultVo EditCareer(@RequestBody EditCareerVo vo){
        if (vo.getId()<=0){
            return ResultVo.createByError("数据不存在或已删除");
        }
        CareerInfo careerInfo= careerInfoService.GetEntityById(vo.getId());
        if (careerInfo==null)
        {
            return ResultVo.createByError("数据不存在或已删除");
        }
        careerInfo.setCityId(vo.getCity_id());
        careerInfo.setQualifiedContent(vo.getQualified_content());
        careerInfo.setTypeId(vo.getType_id());
        careerInfo.setTitle(vo.getTitle());
        careerInfo.setJobId(vo.getJob_id());
        careerInfo.setDutiesContent(vo.getDuties_content());
        careerInfo.setCompany(vo.getCompany());
        if(careerInfoService.update(careerInfo)<=0){
            return ResultVo.createByError("fail");
        }
        return ResultVo.createBySuccessData("succeed");
    }

    @PostMapping("/Career/DelCareer")
    public ResultVo DelCareer(@RequestBody IdsVo vo){
        if (vo.getIds().size()<=0){
            return ResultVo.createByError("请选择要删除的项");
        }
        if(careerInfoService.softDeleteByIds(vo.getIds())<=0){
            return ResultVo.createByError("删除失败");
        }
        return ResultVo.createBySuccessData("succeed");
    }
}
